package com.yonyougov.devops.cruxcisupport.cisupport;

import com.yonyougov.devops.cruxcisupport.appenv.AppEnvService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "部署工具")
@Controller
@RequestMapping(value = "/api/v2")
public class CiSupportController {
    private AppEnvService appEnvService;

    public CiSupportController(AppEnvService appEnvService) {
        this.appEnvService = appEnvService;
    }

    @GetMapping("/deployFile/{appName}/{envName}")
    @ResponseBody
    public String getDeployFile(@PathVariable String appName, @PathVariable String envName) {
        return appEnvService.getDeployFile(appName, envName);
    }

    @GetMapping("/configMap/{envName}")
    @ResponseBody
    public String getConfigMap(@PathVariable String envName) {
        return appEnvService.getConfigMap(envName);
    }

    @ApiOperation(value = "前端环境菜单，部署文件下载调用")
    @GetMapping("/downloadall/{envName}")
    @SneakyThrows
    public void downloadEnv(@PathVariable String envName, HttpServletResponse response) {
        response.setHeader("Context-Type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + envName + ".zip");
        appEnvService.downloadEnv(envName, response.getOutputStream());
    }

    @GetMapping("/app")
    public String redirect1() {
        return "redirect:/";
    }

    @GetMapping("/env")
    public String redirect2() {
        return "redirect:/";
    }

    @GetMapping("/deploy")
    public String redirect3() {
        return "redirect:/";
    }

    @GetMapping("/login")
    public String redirectLogin() {
        return "redirect:https://ci.yonyougov.top/crux-cisupportfe/login";
    }
}
